<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="fileName" placeholder="文件名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="starDate" placeholder="时间" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbar">
            <div class="layuimini-main">
                <button type="button" class="layui-btn" id="uploadBtn"><i class="layui-icon"></i>上传文件</button>
                <a href="#" target="_blank" id="downDemo">下载模板</a>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="view">查看</a>
            {{# if(d.dataStatus === 0) { }}
            <a class="layui-btn layui-btn-xs layui-btn-xs data-count-edit" lay-event="import_data">导入</a>
            <!--<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
            {{# } }}
            <!--{{# if(d.dataStatus === 1) { }}-->
            <!--导入中<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>-->
            <!--{{# } }}-->
            <a class="layui-btn layui-btn-xs layui-btn-primary data-count-delete" lay-event="download">下载</a>
            <!--<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">验证</a>-->
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../js/my.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            upload = layui.upload,
            table = layui.table;
        const host = `${window.location.protocol}//${window.location.host}`;
        $.ajaxSetup({
            headers: {'Access-Token': sessionStorage.getItem('token'), 'userId': sessionStorage.getItem("userId")}
        });



        table.render({
            title: '应用列表',
            elem: '#currentTableId',
            method: 'get',
            url: `${host}/import/page`,
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: '序号'},
                {field: 'batchNo', width: 180, title: '批号'},
                {field: 'fileName', width: 200, title: '文件名'},
                {field: 'fileSize', width: 100, title: '文件大小'},
                {field: 'dataSize', width: 100, title: '导入数据量'},
                {field: 'dataStatus', width: 150, title: '状态', templet: d => {
                        if (d.dataStatus === 0) {
                            return '未导入';
                        }
                        if (d.dataStatus === 1) {
                            return '<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>';
                        }
                        if (d.dataStatus === 2) {
                            return '部分导入';
                        }
                        if (d.dataStatus === 3) {
                            return '完成';
                        }
                        if (d.dataStatus === 4) {
                            return '全部失败';
                        }
                    }},
                {field: 'createTime', width: 180, title: '导入时间'},
                {title: '操作',fixed: 'right', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            parseData: (rest) => {
                return {
                    "code": rest.code,
                    "msg" : rest.msg,
                    "count" : rest.data.total,
                    "data" : rest.data.records
                }
            },
            request: {
                pageName: 'current',
                limitName: 'size'
            }
        });

        $('#downDemo').attr('href', `${host}/file/终端设备导入模板.xlsx`)

        upload.render({ //允许上传的文件后缀
            elem: '#uploadBtn',
            url: `${host}/terminal/upload`, //此处配置你自己的上传接口即可
            accept: 'file', //普通文件
            exts: 'xls|xlsx', //只允许上传压缩文件
            auto: true,//是否自动上传，true自动上传,false手动上传
            size: 50 * 1024,//单位kb
            done: function(res){
                if ("00" === res.code) {
                    layer.msg("上传成功");
                    $(".layui-laypage-btn")[0].click();
                } else {
                    layer.msg(res.msg);
                }

            },
            error: function (index, upload) {
                alert("上传失败！");
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: { current: 1 },
                where: { 'searchParams' : result }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增应用',
                    btn: ['确定','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['80%', '80%'],
                    content: `add.html?timestamp=${new Date().getTime()}`,
                    yes : (index, layero) => {
                        let addData = $(layero).find('iframe')[0].contentWindow.callbackData();
                        $.post(`${host}/app/save`, addData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'import_data') {
                layer.confirm('是否确定导入数据', function (index) {
                    let batchNo = obj.data.batchNo;
                    $.post("/import/import/data", {'batchNo': batchNo}, (rest) => {
                        layer.msg(rest.msg);
                        if (rest && "00" === rest.code) {
                            $(".layui-laypage-btn")[0].click();
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'download'){
               window.open(`${host}/${obj.data.url}`);
            } else if (obj.event === 'view') {
                let viewModel = layer.open({
                    title: '导入记录',
                    btn: ['关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin: false,
                    shadeClose: false,
                    area: ['80%', '80%'],
                    content: `view.html?timestamp=${new Date().getTime()}`,
                    success: (layero, index) => {
                        let body = layer.getChildFrame('body', index);
                        body.find('#batchNo').val(obj.data.batchNo);
                        body.find('#batchNo').click();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });
</script>

</body>
</html>